﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    private static UIManager instance;
    public static UIManager MyInstance
    {
        get
        {
            if (instance == null)
            {
                instance=FindObjectOfType<UIManager>();
            }        
            return instance;
        }
    }
    [SerializeField]
    private CanvasGroup effectDark;
    [SerializeField]
    private CanvasGroup[] windows;
    private GameObject[] buttonBinds;
    [SerializeField]
    private GameObject toolTip;
    [SerializeField]
    private RectTransform toolTipRect;
    private Text descriptionTooltip; 
    [SerializeField]
    private actionButton[] ActionButtons;
    [SerializeField]
    private CharcterPanel cmenu;

    private void Awake()
    {
        buttonBinds = GameObject.FindGameObjectsWithTag("buttonbind");
        descriptionTooltip = toolTip.GetComponentInChildren<Text>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape)) 
        {
            OpenSingle(windows[8]);
        }

        if (Input.GetKeyDown(keybindsManager.MyInstance.KeyBinds["character"]))
        {
            cmenu.Toggle();
        }

        if (Input.GetKeyDown(keybindsManager.MyInstance.KeyBinds["inventory"]))
        {
            inventoryScript.MyInstance.ToggleInv();
        }

        if (Input.GetKeyDown(keybindsManager.MyInstance.KeyBinds["SkillBook"]))
        {
            toggle(windows[0]);
        }

        if (Input.GetKeyDown(keybindsManager.MyInstance.KeyBinds["journal"]))
        {
            toggle(windows[4]);
        }

        if (Input.GetKeyDown(keybindsManager.MyInstance.KeyBinds["CharacterList"]))
        {
            CharList.MyInstance.UpdateAPText();
            toggle(windows[6]);
        }
    }

    public void clickActionButton(string name)
    {
        Array.Find(ActionButtons, x => x.gameObject.name == name).MyButt.onClick.Invoke();
    }

    public void toggle(CanvasGroup cg)
    {
        cg.alpha = cg.alpha > 0 ? 0 : 1;
        cg.blocksRaycasts = cg.blocksRaycasts == true ? false : true;
 
    }

    public void updateButtonText(string key,KeyCode code)
    {
        Text temp=Array.Find(buttonBinds,x=>x.name==key).GetComponentInChildren<Text>();
        temp.text = code.ToString();
    }

   public void UpdateStackSize(IClickable clickable)
    {
        if (clickable.MyCount > 1)
        {
            clickable.MyStackSize.text = clickable.MyCount.ToString();
            clickable.MyStackSize.color = new Color(1, 1, 1, 1);
            clickable.MyIcon.color = new Color(1, 1, 1, 1);
        }
        else
        {
            ClearStackSize(clickable);
        }

        if (clickable.MyCount == 0)
        {
            clickable.MyIcon.color = new Color(0, 0, 0, 0);
            clickable.MyStackSize.color= new Color(0, 0, 0, 0);
        }
    }

    public void showToolTip(Vector2 pivot, Vector3 offset,IDescription description)
    {
        toolTipRect.pivot = pivot;
        toolTip.SetActive(true);
        toolTip.transform.position = offset;
        descriptionTooltip.text = description.Description();
    }

    public void hideToolTip()
    {
        toolTip.SetActive(false);
    }

    public void refreshTooltip(IDescription description)
    {
        descriptionTooltip.text = description.Description();
    }

    public void ClearStackSize(IClickable clickable)
    {
        clickable.MyStackSize.color = new Color(0, 0, 0, 0);
        clickable.MyIcon.color = new Color(1, 1, 1, 1);
    }

    public void OpenSingle(CanvasGroup window)
    {
        foreach(CanvasGroup cg in windows)
        {
            if (cg != windows[8])
            {
                if (Player.MyInstance.MyInteractable != null)
                {
                    Player.MyInstance.MyInteractable.StopInteract();
                }
                else
                {
                    cg.alpha = 0;
                    cg.blocksRaycasts = false;
                }
            }
            
        }
        toggle(window);
        //effectDark.blocksRaycasts = true;
    }
}
